package com.front.web.user.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.front.common.AppPropertiesUtil;
import com.front.common.MD5Util;
import com.front.common.SiteUtil;
import com.front.common.StringUtils;
import com.front.constant.FrontConstant;
import com.front.constant.PayConstant;
import com.front.constant.ReportConstant;
import com.front.web.controller.BaseController;
import com.front.web.shiro.model.User;
import com.front.web.user.model.UserCharge;
import com.front.web.user.service.UserChargeService;
import com.google.gson.Gson;

/***
 * 
 * @author huang
 *
 */
@Controller
public class UserChargeController extends BaseController
{
	@Autowired
    private UserChargeService userChargeService;
      
	@RequestMapping(value="/content/charge/onlineCharge",method={RequestMethod.POST,RequestMethod.GET})
	public void onlineCharge(HttpServletRequest req,HttpServletResponse repson,ModelMap map)throws Exception
	{
		 //String mysign = MD5.encode(fee + "|" + payNo + "|" + uid + "|"+ KEY);
		 User user=super.getLoginUserObject();
		 String bankType = req.getParameter("bank_type"); //获取银行类型
		 String payNo = req.getParameter("payno");//支付的订单号
		 int fee =StringUtils.str2Int(req.getParameter("fee"));
		 StringBuilder sb  = new StringBuilder();
		 sb.append(AppPropertiesUtil.getStringValue(PayConstant.PAY_URL)).append("?");
		 sb.append("fee=").append(fee).append("&");
		 sb.append("bank_type=").append(bankType).append("&");
		 sb.append("payno=").append(payNo).append("&");
		 sb.append("uid=").append(user.getUid()).append("&");
		 String mysign = MD5Util.MD5(fee + "|" + payNo + "|" + user.getUid() + "|"+ AppPropertiesUtil.getStringValue(PayConstant.EXTERNAL_KEY));
		// sb.append("username=").append(user.getLoginName()).append("&");
		 sb.append("sign=").append(StringUtils.lowerCase(mysign));//.append("&");
		 //sb.append("callback=").append(AppPropertiesUtil.getStringValue(PayConstant.PAY_CALLBACK));
		 Map<String,Object>result =  this.buildQueryMap(req);
		 result.put("url", AppPropertiesUtil.getStringValue(PayConstant.PAY_URL));
		 result.put("sign", StringUtils.lowerCase(mysign));
		 result.put("uid", user.getUid());
		 this.renderText(repson, new Gson().toJson(result));
	}
	
	
	@RequestMapping(value="/content/charge/chargeSuccess")
	public void chargeSuccess(HttpServletRequest req,HttpServletResponse repson,ModelMap map)throws Exception
	{
		 String chargeOrder = req.getParameter("chargeOrder"); //获取支付订单
		 UserCharge charge  = userChargeService.queryUserCharge(chargeOrder);
		 if(charge!=null)//该订单已经删除了 
		 {
			 if(charge.getIsDelete()==PayConstant.CHARGE_NORMAL)
			 {			 
			     if(StringUtils.equals(charge.getStatus(),PayConstant.CHARGE_IS_NOTHANDLE))//未支付的订单
			     { 
				     userChargeService.charge(charge);
			     }
			     else
				 {
					 this.renderMsg(repson, FrontConstant.FAILED_CODE, "充值订单【"+chargeOrder+"】已经处理。", StringUtils.EMPTY);
				 }
			 }
			 else
			 {
				 this.renderMsg(repson, FrontConstant.FAILED_CODE, "充值订单【"+chargeOrder+"】已经删除。", StringUtils.EMPTY);
			 }
		 }
		 else
		 {
			 this.renderMsg(repson, FrontConstant.FAILED_CODE, "充值订单【"+chargeOrder+"】错误。", StringUtils.EMPTY);
		 }
	}
	
	
	   @RequestMapping(value="/content/bank/recharge_submit/{chargeId}" , method=RequestMethod.GET)
	    public String ContentRechargeSubmit(HttpServletRequest req,HttpServletResponse resp, ModelMap modelMap,@PathVariable("chargeId")String charge_id)  throws Exception{
	        Map<String, Object>map = new HashMap<String, Object>();
	        map.put("id", charge_id);
	        modelMap.put("charge", this.reportService.querySingleResult("queryChargeDetail", map));
	        return "bank/content-recharge_submit";
	    }
	    
	    @RequestMapping(value="/content/bank/online_recharge/{chargeId}" , method=RequestMethod.GET)
	    public String ContentOnlineRechargeSubmit(HttpServletRequest req,HttpServletResponse resp, ModelMap modelMap,@PathVariable("chargeId")String charge_id)  throws Exception{
	        Map<String, Object>map = new HashMap<String, Object>();
	        map.put("id", charge_id);
	        modelMap.put("charge", this.reportService.querySingleResult("queryChargeDetail", map));
	        return "bank/content-recharge-online";
	    }
	    
	    @RequestMapping("/content/bank/recharge")
	    public String contentRecharge(ModelMap modelMap)  throws Exception{
	    	List<Map> bankList = reportService.getReportResult("queryBankList", null);
	    	modelMap.put("bankList", bankList);
	    	modelMap.put("rechargeLower",reportService.queryCmsConf(ReportConstant.RECHARGE_LOWER));
	    	modelMap.put("rechargeUpper",reportService.queryCmsConf(ReportConstant.RECHARGE_UPPER));
	        return "bank/content-recharge";
	    }
	    
	    @RequestMapping(value="/bank/recharge_submit" , method=RequestMethod.POST)
	    public @ResponseBody UserCharge rechargeSubmit(HttpServletRequest req,HttpServletResponse resp, ModelMap modelMap)  throws Exception{
	    	String bank_id =req.getParameter("bank_id");
	    	String amount = req.getParameter("amount");
	    	String onlinePayment=req.getParameter("onlinePayment");
	        User user = getLoginUserObject();
	        
	        UserCharge userCharge = new UserCharge();
	        userCharge.setChargeType(StringUtils.equals(onlinePayment, "Y")?PayConstant.CHARGE_TYPE_ONLINE:PayConstant.CHARGE_TYPE_OFFLINE);
	        userCharge.setUid(user.getUid());
	        userCharge.setUname(user.getLoginName());
	        userCharge.setAccount(Double.valueOf(amount));
	        userCharge.setStatus("-1");
	        userCharge.setRemark("用户充值");
	        userCharge.setChargeDate(new Date());
	        userCharge.setChargeIp(SiteUtil.getIpAddr());
	        userCharge.setChargeAccount(Integer.valueOf(bank_id));
	        userCharge.setIsDelete(0);
	        
	        userChargeService.insert(userCharge);
	        
	        return userCharge;
	    }
	
}
